package com.hqq.presentation_tier.front_controller;

/**
 * FrontController
 * Created by heqianqian on 2017/8/17.
 */
public class FrontController {

    /**
     * 处理请求
     */
    public void handleRequest(String request) {
        Command command = getCommand(request);
        command.process();
    }

    /**
     * 获取Command实例
     */
    private Command getCommand(String request) {
        Class commandClass = getCommandClass(request);
        try {
            return (Command) commandClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new ApplicationException(e.getMessage());
        }
    }

    /**
     * 加载Command类
     */
    private Class getCommandClass(String request) {
        Class commandClass = null;
        try {
            commandClass = Class.forName("com.hqq.presentation_tier.front_controller." + request + "Command");
        } catch (ClassNotFoundException e) {
            return UnknownCommand.class;
        }
        return commandClass;
    }

}
